##=============================================================================
## Main Manuscript: Figure 1
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "extrafont", "texreg", "sjlabelled", "kableExtra", 
              "ggmap","ggthemes", "sf")

ipak(packages)

##---------
# Load Data
setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##------------------
# Wave 1 (July 2022)
interviews <- dat1 %>% 
  group_by(adm2_en) %>% 
  count() %>% 
  rename(interviews = n)

viina_counts <- dat1 %>% 
  dplyr::select(adm2_en, viina_cum_adm2) %>% 
  distinct(adm2_en, .keep_all = TRUE)

ukr_adm2 <- left_join(ukr_adm2, interviews, by = "adm2_en") 

rm(interviews)

ukr_adm2 <- left_join(ukr_adm2, viina_counts, by = "adm2_en") %>% 
  mutate(viina_cum_adm2 = ifelse(is.na(viina_cum_adm2), 0, viina_cum_adm2))

rm(viina_counts)

ggplot(data = ukr_adm2, aes(fill = viina_cum_adm2, label = interviews)) +
  geom_sf() +
  geom_sf(aes(fill = viina_cum_adm2)) +
  geom_sf_text(aes(label = interviews), colour = "white", 
               fun.geometry = sf::st_centroid) +
  scale_fill_continuous(low = "lightgray", high = "grey10") +
  labs(fill = "Cumulative Combat Events", label = "Interview Count") +
  theme_void() +
  theme(legend.position = "bottom", legend.direction = "horizontal")

ukr_adm2 <- ukr_adm2 %>% 
  select(-c(interviews, viina_cum_adm2))

##-------------------
# Wave 2 (March 2023)
interviews <- dat2 %>% 
  group_by(adm2_en) %>% 
  count() %>% 
  rename(interviews = n)

viina_counts <- dat2 %>% 
  dplyr::select(adm2_en, viina_cum_adm2) %>% 
  distinct(adm2_en, .keep_all = TRUE)

ukr_adm2 <- left_join(ukr_adm2, interviews, by = "adm2_en") %>% 
  mutate(interviews = ifelse(is.na(interviews), 0, interviews))

rm(interviews)

ukr_adm2 <- left_join(ukr_adm2, viina_counts, by = "adm2_en") %>% 
  mutate(viina_cum_adm2 = ifelse(is.na(viina_cum_adm2), 0, viina_cum_adm2))

rm(viina_counts)

ggplot(data = ukr_adm2, aes(fill = viina_cum_adm2, label = interviews)) +
  geom_sf() +
  geom_sf(aes(fill = viina_cum_adm2)) +
  geom_sf_text(aes(label = interviews), colour = "white", 
               fun.geometry = sf::st_centroid) +
  scale_fill_continuous(low = "lightgray", high = "grey10") +
  labs(fill = "Cumulative Combat Events", label = "Interview Count") +
  theme_void() +
  theme(legend.position = "bottom", legend.direction = "horizontal")

ukr_adm2 <- ukr_adm2 %>% 
  select(-c(interviews, viina_cum_adm2))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================